package com.cnhaimei.com;

import android.content.Context;
import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.Transformation;
import android.widget.Gallery;

/**
 * @author Jin001.Wu@gmail.com 
 * @version 2.0
 *
 */

public class GalleryFlow extends Gallery {
    /*
     * Graphics Camera used for transforming the matrix of ImageViews.
     */
    private Camera mCamera = new Camera();

    /*
     * The maximum angle the Child ImageView will be rotated by
     */
    private int mMaxRotationAngle = 60;

    /*
     * The maximum zoom on the centre Child
     */
    private int mMaxZoom = -120;

    /*
     * The Centre of the Coverflow
     */
    private int mCoveflowCenter;

    public GalleryFlow(Context context) {
        super(context);
        this.setStaticTransformationsEnabled(true);
    }

    public GalleryFlow(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.setStaticTransformationsEnabled(true);
    }

    public GalleryFlow(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.setStaticTransformationsEnabled(true);
    }

    /**
     * Get the max rotational angle of the image
     * 
     * @return the mMaxRotationAngle
     */
    public int getMaxRotationAngle() {
        return mMaxRotationAngle;
    }

    /**
     * Set the max rotational angle of each image
     * 
     * @param maxRotationAngle
     *            the mMaxRotationAngle to set
     */
    public void setMaxRotationAngle(int maxRotationAngle) {
        mMaxRotationAngle = maxRotationAngle;
    }

    /**
     * Get the Max zoom of the centre image
     * 
     * @return the mMaxZoom
     */
    public int getMaxZoom() {
        return mMaxZoom;
    }

    /**
     * Set the max zoom of the centre image
     * 
     * @param maxZoom
     *            the mMaxZoom to set
     */
    public void setMaxZoom(int maxZoom) {
        mMaxZoom = maxZoom;
    }

    /**
     * Get the Centre of the Coverflow
     * 
     * @return The centre of this Coverflow.
     */
    private int getCenterOfCoverflow() {
        return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
    }

    /**
     * Get the Centre of the View
     * 
     * @return The centre of the given view.
     */
    private static int getCenterOfView(View view) {
        return view.getLeft() + view.getWidth() / 2;
    }
    
    

    @Override
	protected int getChildDrawingOrder(int childCount, int i) {
		// TODO Auto-generated method stub
    	int selectedIndex = getSelectedItemPosition() - getFirstVisiblePosition();
    	int split = selectedIndex; //childCount / 2 + 1;
    	int rtn = 0;
    	if (i < split ) {
    		rtn =  i;
    	} else if (i == selectedIndex) {
    		rtn = childCount - 1;
    	} else {
    		rtn = childCount - 1 - i + selectedIndex;
    	}
    		
    	
//    	Log.e(this.getClass().getName(), "selectIndex:" + selectedIndex );
    
//    	Log.e(this.getClass().getName(), "childCount:" + childCount + " index:" + i );
		//rtn = super.getChildDrawingOrder(childCount, i);
//		Log.e(this.getClass().getName(), "return:" + rtn); 
		return rtn;
	}
    
    

	/**
     * {@inheritDoc}
     * 
     * @see #setStaticTransformationsEnabled(boolean)
     */
    @Override
    protected boolean getChildStaticTransformation(View child, Transformation t) {

        final int childCenter = getCenterOfView(child);
        final int childWidth = child.getWidth();
        int rotationAngle = 0;
        int centerDiff = childCenter - mCoveflowCenter;

        t.clear();
        t.setTransformationType(Transformation.TYPE_MATRIX);

        if (childCenter == mCoveflowCenter) {
            transformImageBitmap(child, t, 0,centerDiff);
        } else {
            rotationAngle = (int) (((float) (mCoveflowCenter - childCenter) / childWidth) * mMaxRotationAngle);
            if (Math.abs(rotationAngle) > mMaxRotationAngle) {
                rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;
            }
            transformImageBitmap(child, t, rotationAngle,centerDiff);
        }

        return true;
    }
    
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mCoveflowCenter = getCenterOfCoverflow();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void transformImageBitmap(View child, Transformation t
    					, int rotationAngle, int centerDiff) {
        mCamera.save();
        final Matrix imageMatrix = t.getMatrix();
        final int imageHeight = child.getLayoutParams().height;
        final int imageWidth = child.getLayoutParams().width;

        mCamera.translate(-(centerDiff * 0.3f), 0.5f, Math.abs(centerDiff * 0.3f));//100.0f);

        mCamera.getMatrix(imageMatrix);
        imageMatrix.preTranslate(-(imageWidth / 2), -(imageHeight / 2));
        imageMatrix.postTranslate((imageWidth / 2), (imageHeight / 2));
        mCamera.restore();
    }
}
